package com.gohi.main.util.task;

import java.util.Calendar;
import java.util.List;
import java.util.TimerTask;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;

import com.gohi.main.util.StringUtil;
import com.gohi.main.util.Utils;

import com.gohi.main.wxapi.vo.Token;
import com.gohi.main.wxapi.vo.WxAccount;
import com.gohi.main.wxpay.api.WxPayUtil;


/**
 * 继承 定时器任务类
 * 
 */
public class MyTask extends TimerTask {

	public MyTask() {
		super();
	}

	/** 这个代表3点钟的时候执行任务 */
	private static final int C_SCHEDULE_HOUR = 12;

	private static boolean isRunning = false;

	private ServletContext context = null;

	public MyTask(ServletContext context) {
		this.context = context;
	}
	//刷新access_token
	public void run() {
		Calendar cal = Calendar.getInstance();

			     //遍历获取

				//判断不等于空的时候 再获取
				Token t  = com.gohi.main.wxapi.process.WxApi.getToken(WxPayUtil.WX_APPID,WxPayUtil.WXAppSecret);
				//设置
				WxAccount w =new WxAccount();
				//System.err.println(t.getAccessToken());
				w.setAccess_token(t.getAccessToken());
				w.setToken_time(t.getExpiresIn()+"");
				w.setJsapi(t.getJsapiTicket());
				w.setJs_time(t.getExpiresIn());
				w.setLasttime(StringUtil.getTimeSimple());
				WxPayUtil.setAccount(w);
		
		if (!isRunning) {
			if (C_SCHEDULE_HOUR == cal.get(Calendar.HOUR_OF_DAY)) {
				isRunning = true;
				context.log("开始执行指定任务");
				isRunning = false;
				context.log("指定任务执行结束");
	
			}
		} else {
			context.log("上一次任务执行还未结束");
		}
	}

}